home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / ingres04.lzh / source / monitor / ttymon.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-18  |  6.9 KB  |  360 lines

  1. # include    "monitor.h"
  2. # include    <ingres.h>
  3. # include    <version.h>
  4. # include    <opsys.h>
  5. # include    <pv.h>
  6. # include    <func.h>
  7. # include    <signal.h>
  8. # include    <pipes.h>
  9. # include    <setjmp.h>
  10.  
  11. # define    ERRDELIM    '~'
  12.  
  13. /*
  14. **  INTERACTIVE TERMINAL MONITOR
  15. **
  16. **    The monitor gathers text from the standard input and performs
  17. **    a variety of rudimentary editting functions.  This program
  18. **    is the main setup.  Monitor() is then called, which does the
  19. **    real work.
  20. **
  21. **    variables:
  22. **    Nodayfile -- zero prints all messages; positive one suppresses
  23. **        dayfile and logout but not prompts; negative one
  24. **        suppresses all printed material except results from \p.
  25. **    Newline -- set when the last character in the query buffer
  26. **        is a newline.
  27. **    Prompt -- set when a prompt character is needed.
  28. **    Autoclear -- set when the query buffer should be cleared before
  29. **        putting another character in.
  30. **    Nautoclear -- if set, suppresses the autoclear function
  31. **        entirely.
  32. **
  33. **    flags:
  34. **    -M -- trace flag
  35. **    -d -- suppress dayfile
  36. **    -s -- suppress prompt (sets -d)
  37. **    -a -- disable autoclear function
  38. **
  39. **    The last three options can be set by stating "+x".
  40. **
  41. **    Trace Flags:
  42. **        9
  43. **        11 (proc_err)
  44. */
  45.  
  46. extern char    *Usercode;
  47. extern        tm_mon();
  48. extern        tm_init();
  49. extern        tm_intr();
  50. short        tTttymon[100];
  51. extern char    *ztack();
  52. extern jmp_buf    CmReset;
  53.  
  54. struct fn_def    TtyMonFn =
  55. {
  56.     "MONITOR",
  57.     tm_mon,
  58.     tm_init,
  59.     tm_intr,
  60.     NULL,
  61.     0,
  62.     tTttymon,
  63.     100,
  64.     'M',
  65.     0
  66. };
  67.  
  68. tm_init(argc, argv)
  69. int    argc;
  70. char    *argv[];
  71. {
  72.     register int    ndx;
  73.     register char    *p;
  74.     extern void    quit();
  75.     extern void    (*ExitFn)();
  76.     extern int    Equel;
  77.     char        buff[100];
  78.     extern char    *getufield();
  79.     extern char    SysIdent[];
  80.  
  81.  
  82.     /* insure that permissions are ok */
  83.     setuid(getuid());
  84.     setgid(getgid());
  85.  
  86.     setjmp(CmReset);
  87.     signal(SIGPIPE, quit);
  88.  
  89.     ExitFn = quit;
  90.     set_si_buf();
  91.  
  92.     /* process arguments */
  93.     if (!setflag(argv, 'd', 1))
  94.         Nodayfile = 1;
  95.     if (!setflag(argv, 's', 1))
  96.         Nodayfile = -1;
  97.     Nautoclear = !setflag(argv, 'a', 1);
  98.  
  99.     /* preinitialize macros */
  100.     macinit(0, 0, 0);
  101.     macdefine("{pathname}", Pathname, 1);
  102.  
  103.     /* print the dayfile */
  104.     if (Nodayfile >= 0)
  105.     {
  106.         time(buff);
  107.         printf("%s login\n%s", SysIdent, ctime(buff));
  108.     }
  109.     if (Nodayfile == 0 && (Qryiop = fopen(ztack(ztack(Pathname, "/files/dayfile"), VERSION), "r")) != NULL)
  110.     {
  111.         while ((ndx = getc(Qryiop)) > 0)
  112.             putchar(ndx);
  113.         fclose(Qryiop);
  114.     }
  115.  
  116.     /* SET UP LOGICAL QUERY-BUFFER FILE */
  117.     concat("/tmp/INGQ", Fileset, Qbname);
  118.     if ((Qryiop = fopen(Qbname, "w")) == NULL)
  119.         syserr("main: open(%s)", Qbname);
  120.  
  121.     /* GO TO IT ... */
  122.     Prompt = Newline = TRUE;
  123.     Userdflag = Nodayfile;
  124.     Nodayfile = -1;
  125.  
  126.     /* run the system initialization file */
  127.     setjmp(CmReset);
  128.     Phase++;
  129.     include(ztack(Pathname, "/files/startup"));
  130.  
  131.     /* find out what the user initialization file is */
  132.     setjmp(CmReset);
  133.     if (getuser(Usercode, buff) == 0)
  134.     {
  135.         p = getufield(buff, 7);
  136.         if (*p != 0)
  137.             include(p);
  138.     }
  139.     getuser(0, 0);
  140.  
  141.     Nodayfile = Userdflag;
  142.  
  143.     /*
  144.     **  Get user input from terminal
  145.     **
  146.     **    THIS CODE IS A CLUDGE!!!
  147.     **
  148.     **    This code should return right after the setbuf call,
  149.     **    but it doesn't because we want the monitor to be in
  150.     **    control initially.  The way the control module is
  151.     **    written, this will work.  But we are definitely
  152.     **    cheating....
  153.     */
  154.  
  155.     Input = stdin;
  156.     setbuf(stdin, NULL);
  157.     monitor(FALSE);
  158.     quit();
  159. }
  160. /*
  161. **  CATCH SIGNALS
  162. **
  163. **    clear out pipes and respond to user
  164. **
  165. **    Uses trace flag 10
  166. */
  167.  
  168. tm_intr(typ)
  169. int    typ;
  170. {
  171.     register int    i;
  172.  
  173.     if (typ != 2)
  174.         syserr("tm_intr: typ %d", typ);
  175.  
  176.     if (Xwaitpid == 0)
  177.         printf("\nInterrupt\n");
  178.  
  179.     lseek(fileno(stdin), 0L, 2);
  180.     Newline = Prompt = TRUE;
  181.     Nodayfile = Userdflag;
  182.     Oneline = FALSE;
  183.     Idepth = 0;
  184.     setbuf(stdin, NULL);
  185.     Input = stdin;
  186.     xwait();
  187. }
  188. /*
  189. **  PROCESS ERROR MESSAGE
  190. **
  191. **    This routine takes an error message off of the pipe and
  192. **    processes it for output to the terminal.  This involves doing
  193. **    a lookup in the .../files/error? files, where ? is the thous-
  194. **    ands digit of the error number.  The associated error message
  195. **    then goes through parameter substitution and is printed.
  196. **
  197. **    In the current version, the error message is just printed.
  198. **
  199. **    We unquestionably cheat, by doing a longjmp rather than a
  200. **    return here -- this is so that the synchronization works right.
  201. **
  202. **    Trace Flags:
  203. **        30
  204. */
  205.  
  206. proc_err(ppb, pc, pv)
  207. pb_t    *ppb;
  208. int    pc;
  209. PARM    pv[];
  210. {
  211.     register char    c;
  212.     register char    *p;
  213.     int        i;
  214.     char        buf[512];
  215.     int        err;
  216.     FILE        *iop;
  217.     char        *errfilen();
  218.     extern char    *mcall();
  219.     bool        fatal;
  220.     extern jmp_buf    GoJmpBuf;
  221.  
  222.     if (pc <= 0 || pv[0].pv_type != PV_INT)
  223.         syserr("proc_err: pc %d pv0type %d", pc, pv[0].pv_type);
  224.     err = pv[0].pv_val.pv_int;
  225.     Error_id = err;
  226.     fatal = !bitset(PB_INFO, ppb->pb_stat);
  227.  
  228.     /* try calling the {catcherror} macro -- maybe not print */
  229.     p = buf;
  230.     p += smove("{catcherror; ", p);
  231.     p += smove(iocv(err), p);
  232.     p += smove("}", p);
  233.  
  234.     p = mcall(buf);
  235.     if (sequal(p, "0"))
  236.         return (1);
  237.  
  238.     /* open the appropriate error file */
  239.     p = errfilen(err / 1000);
  240.  
  241. #    ifdef xMTR3
  242.     if (tTf(30, -1))
  243.         printf("proc_error: ");
  244.     if (tTf(30, 0))
  245.         printf("%d, %s\n", err, p);
  246. #    endif
  247.  
  248.     if ((iop = fopen(p, "r")) == NULL)
  249.         syserr("proc_error: open(%s)", p);
  250.  
  251.     /* read in the code and check for correct */
  252.     for (;;)
  253.     {
  254.         p = buf;
  255.         while ((c = getc(iop)) != '\t')
  256.         {
  257.             if (c <= 0)
  258.             {
  259.                 /* no code exists, print the args */
  260.                 printf("%d:", err);
  261.                 for (i = 1; i < pc; i++)
  262.                     printf(" `%s'", pv[i].pv_val.pv_str);
  263.                 printf("\n");
  264.                 fclose(iop);
  265.                 if (fatal)
  266.                     longjmp(CmReset, 1);
  267.                 else
  268.                     longjmp(GoJmpBuf, 1);
  269.             }
  270.             *p++ = c;
  271.         }
  272.         *p = 0;
  273.         i = atoi(buf);
  274.  
  275.         if (i != err)
  276.         {
  277.             while ((c = getc(iop)) != ERRDELIM)
  278.                 if (c <= 0)
  279.                     syserr("proc_error: format err %d", err);
  280.             getc(iop);    /* throw out the newline */
  281.             continue;
  282.         }
  283.  
  284.         /* got the correct line, print it doing parameter substitution */
  285.         printf("%d: ", err);
  286.         c = '\n';
  287.         for (;;)
  288.         {
  289.             c = getc(iop);
  290.             if (c <= 0 || c == ERRDELIM)
  291.             {
  292.                 printf("\n");
  293.                 fclose(iop);
  294.                 if (fatal)
  295.                     longjmp(CmReset, 1);
  296.                 else
  297.                     longjmp(GoJmpBuf, 1);
  298.             }
  299.             if (c == '%')
  300.             {
  301.                 c = getc(iop) - '0' + 1;
  302.                 if (c >= pc)
  303.                     syserr("proc_err: parm %d", c - 1);
  304.                 switch (pv[c].pv_type)
  305.                 {
  306.                   case PV_STR:
  307.                     for (p = pv[c].pv_val.pv_str; c = *p; p++)
  308.                         xputchar(c);
  309.                     continue;
  310.  
  311.                   case PV_INT:
  312.                     printf("%d", pv[c].pv_val.pv_int);
  313.                     continue;
  314.  
  315.                   default:
  316.                     syserr("proc_err: arg %d type %d", c, pv[c].pv_type);
  317.                 }
  318.             }
  319.             printf("%c", c);
  320.         }
  321.     }
  322. }
  323. /*
  324. **  TM_MON -- "function to implement this module"
  325. **
  326. **    Since we have cludged up this module to work, and hence
  327. **    the init routine should never return, this routine just
  328. **    syserr's.
  329. */
  330.  
  331. tm_mon()
  332. {
  333.     syserr("tm_mon");
  334. }
  335. /*
  336. **  ACC_INIT, PAGEFLUSH -- dummy access method routines
  337. **
  338. **    Since the CM wants to do some basic access method functions,
  339. **    we will let it.
  340. */
  341.  
  342. acc_init()
  343. {
  344. }
  345.  
  346. pageflush(x)
  347. char    *x;
  348. {
  349.     return (0);
  350. }
  351. /*
  352. **  CLOSECATALOG -- dummy catalog close routine.
  353. **
  354. **    To keep from loading access methods.
  355. */
  356.  
  357. closecatalog()
  358. {
  359. }
  360.